// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var css_detail_info = require("./financingDetailInfo.css");
	var enterpriseId;
	var abnormal;
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var storage = window.sessionStorage;
	var financeId = "";
	var init = function(page) {
		enterpriseId = base.getUrlParam("enterpriseId") || page.params.enterpriseId;
		abnormal =  base.getUrlParam("abnormal") || page.params.abnormal;
		if(enterpriseId==null){
			enterpriseId = storage.getItem("enterpriseId");
		}
		$(".table-section").removeClass("none");
		$("#apply").addClass("leftMenu-active");
		$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
		if (enterpriseId != null) {
			inquireFinanceList(enterpriseId);
		}else {
			inquireFinanceList();
		}

		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#main").on("click", "#detail", function() {
			var financeId = [];
			var $this = $(this).parents("tr.tr_style");
			financeId.push($this.attr("financeId"));
			router.route("financing/financingDetailInfo.html?financeId=" + financeId, true);
		});
		
		$(".transportNoticeList").unbind("click").click(function(){
			alert(123);
		});

		$("#main").on("click", "#financeSearch", function() {
			inquireFinanceList();
		});
	};

	//改变融资状态背景色
	var changeStatusColor = function() {
		var status = $(".order_status");
		for (var i = 0, j = status.length; i < j; i++) {
			switch ($.trim(status[i].innerHTML)) {
				case "融资":
					$(status[i]).css("background-color", "#6BA8E7");
					break;
				case "换货":
					$(status[i]).css("background-color", "#28BF85");
					break;
			}
		}
	};

	//查询融资信息列表
	var inquireFinanceList = function(enterpriseId) {
		if ($("#financeState").val() != -1) {
			var financeState = $("#financeState").val();
		} else {
			var financeState;
		}
		var beginDate = $("#applyTimeStart").val().replace(new RegExp(/-/g), '');
		var endDate = $("#applyTimeEnd").val().replace(new RegExp(/-/g), '');
		var financeNumber = $("#financeNumber").val();
		var maxAmount = $("#maxAmount").val();
		var minAmount = $("#minAmount").val();
		var commodityName = $("#commodityName").val();

		base.fnAjax({
			action: "inquireFinanceList.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				financeState: financeState,
				beginDate: beginDate,
				endDate: endDate,
				financeNumber: financeNumber,
				maxAmount: maxAmount,
				minAmount: minAmount,
				commodityName: commodityName,
				enterpriseId: enterpriseId,
				abnormal: abnormal
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					if(abnormal == 0){
						var tpl = $("#financeListTpl1").html();
					}else{
						var tpl = $("#financeListTpl").html();
					}
					var html = juicer(tpl, data);
					$("#financeListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#financeManageList .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							if (enterpriseId != null) {
								inquireFinanceList(enterpriseId);
							} else {
								inquireFinanceList();
							}
						});
					changeStatusColor();
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});